from sqlalchemy import Column, Integer, String

from core.base.base_model import BaseModel
from core.service.services_email import EmailServie


class Employee(BaseModel):
    __tablename__ = 'employee'  # 表名

    name = Column(String(50))
    age = Column(Integer)
    sex = Column(String(10))
    photo = Column(String(100))
    email = Column(String(100))
    password = Column(String(50))

    @classmethod
    def register_employee(cls,code,email_str,password):
        # 查询是否存在
        user = cls.db().query(cls.id).filter(
            cls.email == email_str
        ).first()

        if user:
            return False
        
        # 验证邮箱验证码
        if EmailServie.is_right_code(code,email_str):
            return cls.create_data({
                "email":email_str,
                "password":password
            })
        return True

    @classmethod
    def login_employee(cls,password,email_str=None,phone=None):
        employee = cls.db().query(cls.id).filter(
            cls.password == password
        )
        if phone:
            employee.filter(cls.phone == phone)
        if email_str:
            employee.filter(cls.email == email_str)
        employee = employee.first()

        res = {}
        if employee:
            res = Employee.super_get_json(employee.id)

        return res

